<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 后台线路订单管理控制器
 * Created by PhpStorm.
 * User: 66diqiu-li
 * Date: 14-8-1
 * Time: 上午9:18
 */
class Order_Admin_module extends CI_Module {


    /**
     * 站点配置ID
     *
     * @var string
     */
    private $data;

    /**
     * 站点配置ID
     *
     * @var string
     */
    private $sid = '';
    private $order_status_array;
    private $pay_status_array;

    /**
     * 构造函数
     */
    function __construct()
    {
        parent::__construct();
        $this->sid = $GLOBALS['sid'];
        $this->data = $GLOBALS['config'];
        $this->load->model('order.Orderlog_model');
        $this->load->model('order.Orderpaylog_model');
        $this->load->model('order.Ordertourist_model');
        $this->load->model('order.Ordercontacts_model');
        $this->load->model('order.Orderinfo_model');
        $this->load->model('member.User_model');
        $this->load->model('admin.area_model');

        $this->load->model('order.Orderhotel_model');

        $this->load->model('admin.Adminuser_model');
        $this->load->model('admin.Adminusercashmsg_model');
        $this->load->model('admin.Adminrole_model');
        $this->load->model('expert.Expert_model');
        $this->load->model('member.User_model');


        $this->config->load('base');

        $this->order_status_array = $this->config->item('order_status');
        $this->pay_status_array = $this->config->item('pay_status');

        $this->Adminuser_model->is_login();

    }


   



    public function tj_order()
    {
        $this->data['title'] = '订单统计';
        $this->data['view'] = 'tj_order';


        $startdate = $this->input->get_post('start_date');
        $enddate = $this->input->get_post('end_date');
        if(empty($enddate))$enddate = date('Y-m-d',strtotime("today"));
        if(empty($startdate))$startdate= date('Y-m-d',strtotime("-1 month"));


        $condition['startdate'] = $startdate;
        $condition['enddate']   = $enddate;
        $this->data['condition'] = $condition;
        $starttime=strtotime($startdate);
        $endtime=strtotime($enddate);
        $end_day=round(($endtime-$starttime)/86400);
        $add = date('j',$starttime);

        $date_array = array();
        for($day=$add; $day<=$end_day+$add; $day++){
            $temp_date = date("Y-m-d",mktime(0,0,0,date('m',$starttime),$day,date('Y',$starttime)));//
            array_push($date_array,$temp_date);
        }
        $cattype = array( 1 );
        $kk = count($date_array) -1;

        $data_array1 = $this->Orderinfo_model->fetch_count($date_array, array(1), $cattype);
        $data_array1[$kk] = $this->Orderinfo_model->fetch_count_by_date($date_array[$kk], '', array(1), $cattype);

        $data_array2 = $this->Orderinfo_model->fetch_count($date_array, array(2), $cattype);
        $data_array2[$kk] = $this->Orderinfo_model->fetch_count_by_date($date_array[$kk], '', array(2), $cattype);

        $data_array6 = $this->Orderinfo_model->fetch_count($date_array, array(6), $cattype);
        $data_array6[$kk] = $this->Orderinfo_model->fetch_count_by_date($date_array[$kk], '', array(6), $cattype);

        $this->data['kk'] = $kk;
        $this->data['date_array'] = $date_array;
        $this->data['data_array1'] = $data_array1;
        $this->data['data_array2'] = $data_array2;
        $this->data['data_array6'] = $data_array6;

        $this->loadview($this->data, 'admin/tj_order');

    }





    /**
     *  获取支付页面 以及支付日志
     */
    public function ajax_pay_account()
    {
        $order_id = $this->input->get('id');
        $type = $this->input->get('type') ? $this->input->get('type') : '1';// 默认是后台付款

        //订单基本信息
        $order_row = $this->Orderinfo_model->get_row(array('order_id' => $order_id));

        $data['list'] = $this->Orderpaylog_model->get_list(array('order_id' => $order_id));
        $data['type'] = $type;
        $data['order_info'] = $order_row;
        $data['save_pay_account_url'] = site_url('order/admin/save_pay_account');
        $this->load->view('admin/ajax_pay_account', $data);
    }

    /**
     * 获取调账页面
     */
    public function ajax_tz()
    {
        $order_id = $this->input->get_post('id');
        $data['order_id'] = $order_id;
        $data['index_url'] = site_url('order/admin/ajax_tz_save');
        $data['return_url'] = site_url('order/admin/dis_edit?id=' . $order_id);
        $this->load->view('admin/order/ajax_tz', $data);
    }

    public function ajax_tz_save()
    {
        $post_data = $this->input->get_post('Form');
        $post_data['create_date'] = date('Y-m-d H:i:s', now());
        $post_data['create_person'] = $this->session->userdata('admin_user_name');
        $return_url = $this->input->get_post('return_url');

        $pay_status = 2;
        $order_row = $this->Orderinfo_model->get_row(array('order_id' => $post_data['order_id']), 'order_id,order_account,paid_account,pay_status');
        if ($order_row['order_account'] + $post_data['account'] == 0)
        {
            $this->success('操作失败,调帐后订单金额不能为0', $return_url, true);
        }
        if ($order_row['order_account'] + $post_data['account'] < $order_row['paid_account'])
        {
            $this->success('操作失败,调帐后不能小于已支付金额', $return_url, true);
        }
        if ($order_row['order_account'] + $post_data['account'] == $order_row['paid_account'])
        {
            $pay_status = 3;
        }
        if ($order_row['paid_account'] == 0)
        {
            $pay_status = 1;
        }
//        if($order_row['order_account']+$post_data['account']==$order_row['paid_account']){
//            $pay_status = 2;
//        }

        $res = $this->Tz_model->add_row($post_data);
        if ($res)
        {
            $this->Orderinfo_model->order_tz($post_data['order_id'], $post_data['account'], $pay_status);
            $this->success('操作成功', $return_url, true);
        }
        else
        {
            $this->success('操作失败', $return_url, true);
        }


    }

    /**
     * 保存支付信息
     */
    public function save_pay_account()
    {
        $arg_post = $this->input->post();
        if (!intval($arg_post['pay_account'])) $this->error('金额不能为0，支付失败!', 0, true);
        $arg_post['status'] = 2;
        $arg_post['type'] = 3; // 后台支付

        $return_url = $arg_post['return_url'];

        $order_row = $this->Orderinfo_model->get_row(array('order_id' => $arg_post['order_id']), 'order_type,order_status,order_type,pay_status,paid_account');

        if ($order_row['pay_stauts'] == 3 && $arg_post['pay_account'] > 0)
        {
            $this->error('重复提交,订单支付失败!', $return_url, true);
        }
        $res = $this->Orderpaylog_model->save_pay_account($arg_post);

        if ($res)
        {

            $this->success('订单支付成功!', $return_url, true);
        }
        else
        {
            $this->error('订单支付失败!', $return_url, true);
        }
    }

    /**
     * ajax订单状态
     */
    public function ajax_order_status()
    {
        $order_id = $this->input->get('order_id');
        $status = $this->input->get('status');
        $data['order_id'] = $order_id;
        $data['status'] = $status;
        $data['save_order_status_url'] = site_url('order/admin/save_order_status');
        $this->load->view('admin/ajax_order_status', $data);
    }

    /**
     * 订单操作状态保存
     */
    public function save_order_status()
    {
        $arg_post = $this->input->post();

        $order_row = $this->Orderinfo_model->get_row(array('order_id' => $arg_post['order_id']), 'order_type,order_status,pay_status');


        // 取消订单
        if ($arg_post['status'] == 3)
        {
            $data = array('order_id' => $arg_post['order_id'], 'order_status' => 3, 'note' => $arg_post['note']);
        }

        // 审核订单

        if ($order_row['order_status'] == 1 && $arg_post['status'] == 2)
        {
            $data = array('order_id' => $arg_post['order_id'], 'order_status' => 2, 'note' => $arg_post['note']);
        }

        // 已入住订单

        if ($order_row['order_status'] == 4 && $arg_post['status'] == 5)
        {
            $data = array('order_id' => $arg_post['order_id'], 'order_status' =>5, 'note' => $arg_post['note']);
        }

        // 完成订单

        if ($order_row['order_status'] == 5 && $order_row['pay_status'] == 3 && $arg_post['status'] == 6)
        {
            $data = array('order_id' => $arg_post['order_id'], 'order_status' => 6, 'note' => $arg_post['note']);
        }

        $res = $this->Orderinfo_model->translate_order_status($data);


        $message = '';
        if ($arg_post['status'] == 2)
        {//审核订单
            $message = '审核';
        }
        elseif ($arg_post['status'] == 3)
        {//取消订单
            $message = '取消';
        }
        elseif ($arg_post['status'] == 6)
        {//完成订单
            $message = '完成';
        }
        $return_url = $this->input->get_post('return_url');


        if ($res)
        {


            if($arg_post['status'] == 2 )
            {
                $order_room = $this->Orderhotel_model->get_row(array('order_id' => $arg_post['order_id']), 'number');
                $order_contact = $this->Ordercontacts_model->get_row(array('order_id' => $arg_post['order_id']), 'phone_num');

                $this->User_model->_send_sms($order_contact['phone_num'],2017,$order_room['number'].'人');
            }

            $this->success('该订单' . $message . '成功,插入日志成功!', $return_url, true);
        }
        else
        {
            $this->error('该订单' . $message . '失败,插入日志失败!', $return_url, true);
        }
    }




    /**
     * 订单短信通知
     */
    public function send_order_msg()
    {

        $post = $this->input->post();
        $order_id = $post['id'];
        $return = $this->order_info($order_id);
        $result = $this->User_model->_send_sms($return['mobile'], $return['template']);
        if ($result)
        {
            $where = array('order_id' => $order_id);
            $data = array('msg_status' => '1');
            $res = $this->Orderinfo_model->edit_row($data, $where);
            if ($res)
            {
                $this->success('发送成功', '', true);
            }
            else
            {
                $this->error('发送失败', '', true);
            }
        }
    }


    /**
     * 订单详情
     *
     * @param order_id 订单ID
     *
     * @return array 返回数据
     */

    public function order_info($order_id)
    {
        $return = array();
        $order_info = $this->Orderinfo_model->get_row(array('order_id' => $order_id));
        $contacts_info = $this->Ordercontacts_model->get_row(array('order_id' => $order_id));
        if ($order_info['order_type'] == '1')
        {
            $template = $this->data['line_order_template_pay'];
            $order_detail = $this->Ordergodate_model->get_row(array('order_id' => $order_id));
            $return['order_sn'] = $order_info['order_sn'];
            $return['order_title'] = $order_detail['line_name'];
            $return['order_date'] = $order_detail['go_date'];
            $return['order_num'] = $order_detail['total_num'] . '人';
            $return['order_type'] = $order_info['order_type'];
            $return['cat_type'] = $order_info['cat_type'];
            $return['order_status'] = $order_info['order_status'];
            $return['mobile'] = $contacts_info['phone_num'];
            $return['pay_status'] = $order_info['pay_status'];
            $return['order_account'] = $order_info['order_account'];
            $return['paid_account'] = $order_info['paid_account'];
            $return['redirect'] = site_url('order/index/line_info?id=' . $order_id);
            $return['goods_url'] = site_url("xianlu/{$order_detail['line_id']}.html");
            $target = array(
                '{$order_sn}',
                '{$order_account}',
                '{$line_name}',
                '{$go_date}',
                '{$contact_name}',
                '{$contact_phone}'
            );
            $replace = array(
                $order_info['order_sn'],
                $order_info['order_account'],
                $order_detail['line_name'],
                $order_detail['go_date'],
                $contacts_info['name'],
                $contacts_info['phone_num']
            );
            $return['template'] = str_replace($target, $replace, $template);
        }

        if ($order_info['order_type'] == '2')
        {
            $template = $this->data['ticket_order_template_pay'];
            $table = $this->db->dbprefix("order_tickets");
            $order_detail = $this->Orderticket_model->get_row(array($table . '.order_id' => $order_id));
            $return['order_sn'] = $order_info['order_sn'];
            $return['order_title'] = $order_detail['scenic_name'] . $order_detail['name'];
            $return['order_date'] = $order_detail['user_date'];
            $return['order_num'] = $order_detail['num'] . '张';
            $return['order_type'] = $order_info['order_type'];
            $return['cat_type'] = $order_info['cat_type'];
            $return['order_status'] = $order_info['order_status'];
            $return['mobile'] = $contacts_info['phone_num'];
            $return['pay_status'] = $order_info['pay_status'];
            $return['order_account'] = $order_info['order_account'];
            $return['paid_account'] = $order_info['paid_account'];
            $return['redirect'] = site_url('order/index/ticket_info?id=' . $order_id);
            $return['goods_url'] = site_url("jingdian/{$order_detail['scenic']}.html");
            $target = array(
                '{$order_sn}',
                '{$order_account}',
                '{$scenic_name}',
                '{$ticket_name}',
                '{$user_date}',
                '{$num}',
                '{$contact_phone}'
            );
            $replace = array(
                $order_info['order_sn'],
                $order_info['order_account'],
                $order_detail['scenic_name'],
                $order_detail['name'],
                $order_detail['user_date'],
                $order_detail['num'],
                $contacts_info['phone_num']
            );
            $return['template'] = str_replace($target, $replace, $template);
        }

        return $return;
    }


    public function dis_order_pay()
    {

        $order_id = $this->input->get_post('order_id');
        $order_row = $this->Orderinfo_model->get_row(array('order_id' => $order_id), 'order_id,order_sn,order_type,order_status,pay_status,order_account as pay_account');

        $pay_data = array(
            'order_id' => $order_id,
            'order_type' => $order_row['order_type'],
            'order_status' => $order_row['order_status'],
            'pay_status' => $order_row['pay_status'],
            'pay_account' => $order_row['pay_account'],
            'status' => 2,
            'type' => 1,
            'note' => '余额支付'
        );

        $res = $this->Orderpaylog_model->save_pay_account($pay_data);
        $return_url = site_url('order/admin/dis_edit?id=' . $order_id);
        if ($res)
        {
            $pay_log = array();
            $pay_log['cash_type'] = 2;
            $pay_log['money'] = $order_row['pay_account'];
            $pay_log['remark'] = '支付订单' . $order_row['order_sn'];
            $pay_log['user_id'] = $pay_log['add_admin_id'] = $this->session->userdata('admin_user_id');
            $pay_log['add_admin_username'] = $this->session->userdata('admin_user_name');
            $pay_log['add_time'] = date('Y-m-d H:i:s', now());
            $pay_log['sid'] = $this->sid;
            $pay_log['type'] = 2;
            $this->Adminuser_model->add_cash_log($pay_log);
            $this->success('订单支付成功!', $return_url, 3);
        }
        else
        {
            $this->error('订单支付失败!', $return_url, 3);
        }

    }

    function hetong()
    {
        $order_id = $this->input->get('id');
        $hetong = $this->Hetong_model->get_row(array('order_id' => $order_id));
        $order = $this->Orderinfo_model->get_hetong_order($order_id);
//        print_r($hetong);
        $data['order'] = $order;
        $data['row'] = $hetong;
        $template = empty($hetong) ? 'admin/order/hetong' : 'admin/order/hetong_dayin';
        $this->load->view($template, $data);
    }

    function save_hetong()
    {
        $data = $this->input->post("Form");
        $data['admin_id'] = $this->session->userdata('admin_user_id');
        $data['create_date'] = date('Y-m-d H:i:s');
        $data['add_date'] = date('Y-m-d H:i:s');
        $res = $this->Hetong_model->add_row($data);
        if ($res)
        {
            $this->success('合同提交成功!', site_url('order/admin/line_index?cat_type=8'), true);
        }
    }

    function download($id)
    {
        header("Content-Type: application/octet-stream");
        $filename = date("Y-m-d-") . $id . ".doc";
        if (preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT']))
        {
            header('Content-Disposition:  attachment; filename="' . $filename . '"');
        }
        elseif (preg_match("/Firefox/", $_SERVER['HTTP_USER_AGENT']))
        {
            header('Content-Disposition: attachment; filename*="utf8' . $filename . '"');
        }
        else
        {
            header('Content-Disposition: attachment; filename="' . $filename . '"');
        }

        $hetong = $this->Hetong_model->get_row(array('order_id' => $order_id));
        $data['row'] = $hetong;
        $str = $this->load->view('admin/order/hetong_dayin', $data);
        echo $str;
    }

    /**
     * 供应商金额提交申请.
     */
    public function ajax_admin_user_msg()
    {
        $order_id = $this->input->get('id');
        $data = array();
        $data['order_id'] = $order_id;
        $data['save_admin_user_cash_msg_url'] = site_url('order/admin/save_admin_user_cash_msg');
        $this->load->view('admin/order/adminUserCashMsg', $data);
    }

    /**
     * 提交申请保存
     */
    public function save_admin_user_cash_msg()
    {
        $arg_post = $this->input->post('Form');
        $order_id = $this->input->post('order_id');
        $order_row = $this->Orderinfo_model->get_row(array('order_id' => $order_id), 'order_account,paid_account,order_sn');
        $arg_post['user_id'] = $this->session->userdata('admin_user_id');
        $arg_post['user_name'] = $this->session->userdata('admin_user_name');
        $arg_post['remark'] .= ' 订单信息:编号为' . $order_row['order_sn'] . ',订单总金额为:' . $order_row['order_account'] . ',未支付金额为:' . ($order_row['order_account'] - $order_row['paid_account']);
        $res = $this->Adminusercashmsg_model->add_row($arg_post);
        $return_url = site_url('order/admin/dis_edit?id=' . $order_id);
        if ($res)
        {
            $this->success('申请提交成功!', $return_url, true);
        }
        else
        {
            $this->error('申请提交失败!', $return_url, true);
        }
    }

    /**
     * 打款到供应商
     */
    function ajax_finacial_status()
    {
        $order_id = $this->input->get_post('order_id');
        $status = $this->input->get_post('status');
        $status = empty($status) ? 2 : $status;
        $res = $this->Orderinfo_model->edit_row(array('finacial_status' => $status), array('order_id' => $order_id));
        if ($res)
        {
            $return = array('code' => 1, 'msg' => '操作成功');
            echo json_encode($return);
            exit;
        }
        else
        {
            $return = array('code' => 0, 'msg' => '操作失败');
            echo json_encode($return);
            exit;
        }

    }




}